// Class1.cs
//

using System;
using System.DHTML;
using Sys;
using seh = Sys.EventHandler;
using sea = Sys.EventArgs;
using SilverlightControlToolkit;
using System.Silverlight.Presentation;

namespace SilverlightControlToolkit.SampleScripts
{
    /// <summary>
    /// The easiest way to use SliverlightControlToolkit, is to write a class inheriting form SLManager
    /// and use the MS Ajax $create(...) syntax to instanciate it.
    /// Script# provides a very productive way to do so, as it provides the same tooling / intellisense / compile-time checkings as C#
    /// </summary>
    public class SampleManager : SLManager
    {
        Button m_myBtn;
        Button m_myBtn1;
        Button m_myBtn2;
        ToggleButton m_toggle;

        ToggleButtonList m_tbList;
        ToggleButtonList m_tbList2;

        TextBlock  m_debugBox;
        StackPanel m_stackPanel;

        AgBehavior center;
        AgBehavior top;
        AgBehavior bottom;
        AgBehavior left;
        AgBehavior right;
        Grid gd;

        public SampleManager(DOMElement host)
            : base(host)
        {
        }
        /// <summary>
        /// This method is called when the Silverlight plugin is loaded and the xaml page is ready to use
        /// </summary>
        /// <param name="slControl"></param>
        /// <param name="args"></param>
        public override void OnSilverlightLoaded(System.Silverlight.SilverlightControl slControl, object args)
        {
            m_debugBox = (TextBlock) Plugin.Content.FindName("txtDebug");

            // Animation templates for buttons, radios, checkboxes...
            // note the $name$ and $targetname$ instances. They are replaced by SilverlightControlToolkit when applied

            string mouseEnterTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\">";
            mouseEnterTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_textBlock\" Storyboard.TargetProperty=\"(TextBlock.Foreground).(SolidColorBrush.Color)\">";
            mouseEnterTemplate += "<SplineColorKeyFrame KeyTime=\"00:00:00.6000000\" Value=\"#FF00FF00\"/>";
            mouseEnterTemplate += "</ColorAnimationUsingKeyFrames>";
            mouseEnterTemplate += "</Storyboard>";
            string mouseLeaveTemplate = "<Storyboard  xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\">";
            mouseLeaveTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_textBlock\" Storyboard.TargetProperty=\"(TextBlock.Foreground).(SolidColorBrush.Color)\">";
            mouseLeaveTemplate += "	<SplineColorKeyFrame KeyTime=\"00:00:00.6000000\" Value=\"#FF000000\"/>";
            mouseLeaveTemplate += "</ColorAnimationUsingKeyFrames>";
            mouseLeaveTemplate += "</Storyboard>";
            string mouseDownTemplate = "<Storyboard  xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"  x:Name=\"$name$\">";
            mouseDownTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangle\" Storyboard.TargetProperty=\"(Shape.Fill).(SolidColorBrush.Color)\">";
            mouseDownTemplate += "<SplineColorKeyFrame KeyTime=\"00:00:00.1000000\" Value=\"#FFF2FF00\"/>";
            mouseDownTemplate += "</ColorAnimationUsingKeyFrames>";
            mouseDownTemplate += "</Storyboard>";
            string mouseUpTemplate = "<Storyboard  xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"  x:Name=\"$name$\">";
            mouseUpTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangle\" Storyboard.TargetProperty=\"(Shape.Fill).(SolidColorBrush.Color)\">";
            mouseUpTemplate += "<SplineColorKeyFrame KeyTime=\"00:00:00.1000000\" Value=\"#FFFF0000\"/>";
            mouseUpTemplate += "</ColorAnimationUsingKeyFrames>";
            mouseUpTemplate += "</Storyboard>";

            string radioEnterTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\">";
            radioEnterTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellipseOuter\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">";
			radioEnterTemplate+="	<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF4E6D78\"/>";
			radioEnterTemplate+="</ColorAnimationUsingKeyFrames>";
            radioEnterTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeInner\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">";
			radioEnterTemplate+="	<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF6F9EAF\"/>";
			radioEnterTemplate+="</ColorAnimationUsingKeyFrames>";
            radioEnterTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeInner\" Storyboard.TargetProperty=\"(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)\">";
            radioEnterTemplate += "<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF8E9DC7\"/>";
            radioEnterTemplate += "</ColorAnimationUsingKeyFrames>";
            radioEnterTemplate += "</Storyboard>";

            string radioLeaveTemplate="<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\">";
            radioLeaveTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellipseOuter\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">";
			radioLeaveTemplate+="<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF767676\"/>";
			radioLeaveTemplate+="</ColorAnimationUsingKeyFrames>";
            radioLeaveTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeInner\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">";
			radioLeaveTemplate+="<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FFADADAD\"/>";
			radioLeaveTemplate+="</ColorAnimationUsingKeyFrames>";
            radioLeaveTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeInner\" Storyboard.TargetProperty=\"(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)\">";
            radioLeaveTemplate += "<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FFC8C8C8\"/>";
            radioLeaveTemplate += "</ColorAnimationUsingKeyFrames>";
            radioLeaveTemplate += "</Storyboard>";

            string radioDownTemplate="<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\">";
            radioDownTemplate += "<DoubleAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeSelector\" Storyboard.TargetProperty=\"(UIElement.Opacity)\">";
			radioDownTemplate+="<SplineDoubleKeyFrame KeyTime=\"00:00:00.2000000\" Value=\"1\"/>";
			radioDownTemplate+="</DoubleAnimationUsingKeyFrames>";
            radioDownTemplate += "</Storyboard>";

            string radioUpTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\">";
            radioUpTemplate += "<DoubleAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeSelector\" Storyboard.TargetProperty=\"(UIElement.Opacity)\">";
			radioUpTemplate+="<SplineDoubleKeyFrame KeyTime=\"00:00:00.2000000\" Value=\"0\"/>";
			radioUpTemplate+="</DoubleAnimationUsingKeyFrames>";
            radioUpTemplate += "</Storyboard>";


            // Checkboxes : 

            string checkEnterTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\">";
            checkEnterTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleOuter\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">";
            checkEnterTemplate += "	<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF4E6D78\"/>";
            checkEnterTemplate += "</ColorAnimationUsingKeyFrames>";
            checkEnterTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleInner\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">";
            checkEnterTemplate += "	<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF6F9EAF\"/>";
            checkEnterTemplate += "</ColorAnimationUsingKeyFrames>";
            checkEnterTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleInner\" Storyboard.TargetProperty=\"(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)\">";
			checkEnterTemplate += "<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF8E9DC7\"/>";
            checkEnterTemplate += "</ColorAnimationUsingKeyFrames>";
            checkEnterTemplate += "</Storyboard>";
          
            string checkLeaveTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\">";
            checkLeaveTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleOuter\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">";
            checkLeaveTemplate += "<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF767676\"/>";
            checkLeaveTemplate += "</ColorAnimationUsingKeyFrames>";
            checkLeaveTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleInner\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">";
            checkLeaveTemplate += "<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FFADADAD\"/>";
            checkLeaveTemplate += "</ColorAnimationUsingKeyFrames>";
            checkLeaveTemplate += "<ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleInner\" Storyboard.TargetProperty=\"(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)\">";
            checkLeaveTemplate += "<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FFC8C8C8\"/>";
            checkLeaveTemplate += "</ColorAnimationUsingKeyFrames>";
            checkLeaveTemplate += "</Storyboard>";

            string checkDownTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\">";
            checkDownTemplate += "<DoubleAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_pathSelector\" Storyboard.TargetProperty=\"(UIElement.Opacity)\">";
            checkDownTemplate += "<SplineDoubleKeyFrame KeyTime=\"00:00:00.2000000\" Value=\"1\"/>";
            checkDownTemplate += "</DoubleAnimationUsingKeyFrames>";
            checkDownTemplate += "</Storyboard>";

            string checkUpTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\">";
            checkUpTemplate += "<DoubleAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_pathSelector\" Storyboard.TargetProperty=\"(UIElement.Opacity)\">";
            checkUpTemplate += "<SplineDoubleKeyFrame KeyTime=\"00:00:00.2000000\" Value=\"0\"/>";
            checkUpTemplate += "</DoubleAnimationUsingKeyFrames>";
            checkUpTemplate += "</Storyboard>";


            base.OnSilverlightLoaded(slControl, args);
            
            // Using script#, I prefer creating objects using property accessors
            // than Application.Create as Application.Create is not type safe and does not supports C# refactoring etc.
            m_myBtn = new Button();
            m_myBtn.Plugin = this.Plugin;
            m_myBtn.ElementName = "MyButton";
            m_myBtn.add_click(new seh( onBtnClick));
            m_myBtn.MouseEnterTemplate = mouseEnterTemplate;
            m_myBtn.MouseLeaveTemplate = mouseLeaveTemplate;
            m_myBtn.MouseDownTemplate = mouseDownTemplate;
            m_myBtn.MouseUpTemplate = mouseUpTemplate;
            m_myBtn.Initialize();
            Application.AddComponent(m_myBtn);

            m_myBtn1 = new Button();
            m_myBtn1.Plugin = this.Plugin;
            m_myBtn1.ElementName = "MyButton1";
            m_myBtn1.add_click(new seh(onBtnClick));
            m_myBtn1.MouseEnterTemplate = mouseEnterTemplate;
            m_myBtn1.MouseLeaveTemplate = mouseLeaveTemplate;
            m_myBtn1.MouseDownTemplate = mouseDownTemplate;
            m_myBtn1.MouseUpTemplate = mouseUpTemplate;
            m_myBtn1.Initialize();
            Application.AddComponent(m_myBtn1);

            m_myBtn2 = new Button();
            m_myBtn2.Plugin = this.Plugin;
            m_myBtn2.ElementName = "MyButton2";
            m_myBtn2.add_click(new seh(onBtnClick));
            m_myBtn2.MouseEnterTemplate = mouseEnterTemplate;
            m_myBtn2.MouseLeaveTemplate = mouseLeaveTemplate;
            m_myBtn2.MouseDownTemplate = mouseDownTemplate;
            m_myBtn2.MouseUpTemplate = mouseUpTemplate;
            m_myBtn2.Initialize();
            Application.AddComponent(m_myBtn2);

            m_toggle = new ToggleButton();
            m_toggle.Plugin = this.Plugin;
            m_toggle.ElementName ="ToggleButton";
            m_toggle.add_click(new seh(onBtnClick));
            m_toggle.add_pushed(new seh(onPushed));
            m_toggle.add_unpushed(new seh(onUnPushed));
            m_toggle.MouseEnterTemplate = mouseEnterTemplate;
            m_toggle.MouseLeaveTemplate = mouseLeaveTemplate;
            m_toggle.MouseDownTemplate = mouseDownTemplate;
            m_toggle.MouseUpTemplate = mouseUpTemplate;
            m_toggle.Initialize();
            Application.AddComponent(m_toggle);

            ArrayList toggleButtons = new ArrayList();
            for (int i = 0; i < 4; i++)
            {
                ToggleButton tb = new ToggleButton();
                tb.Plugin = this.Plugin;
                tb.ElementName = "Radio" + i.ToString();
                tb.MouseEnterTemplate = radioEnterTemplate;
                tb.MouseLeaveTemplate = radioLeaveTemplate;
                tb.MouseDownTemplate = radioDownTemplate;
                tb.MouseUpTemplate = radioUpTemplate;
                tb.Initialize();
                Application.AddComponent(tb);
                ArrayList.Add(toggleButtons, tb);
            }
            m_tbList = new ToggleButtonList();
            m_tbList.Plugin = Plugin;
            m_tbList.Items = toggleButtons;
            m_tbList.Behavior = ToggleButtonListBehavior.RadioButtonList;
            m_tbList.ID = "ToggleButtonList0";
            m_tbList.Initialize();
            m_tbList.add_selectionChanged(new seh(onSelectionChanged));
            Application.AddComponent(m_tbList);

            ArrayList checks = new ArrayList();
            for (int i = 0; i < 4; i++)
            {
                ToggleButton tb = new ToggleButton();
                tb.Plugin = Plugin;
                tb.ElementName = "Check" + i.ToString();
                tb.MouseEnterTemplate = checkEnterTemplate;
                tb.MouseLeaveTemplate = checkLeaveTemplate;
                tb.MouseDownTemplate = checkDownTemplate;
                tb.MouseUpTemplate = checkUpTemplate;
                tb.Initialize();
                Application.AddComponent(tb);
                ArrayList.Add(checks, tb);
            }
            m_tbList2 = new ToggleButtonList();
            m_tbList2.Plugin = Plugin;
            m_tbList2.Items = checks;
            m_tbList2.Behavior = ToggleButtonListBehavior.CheckboxButtonList;
            m_tbList2.ID = "ToggleButtonList1";
            m_tbList2.Initialize();
            m_tbList2.add_selectionChanged(new seh(onSelectionChanged));
            Application.AddComponent(m_tbList2);
            m_stackPanel = new StackPanel();
            m_stackPanel.Plugin = Plugin;
            m_stackPanel.ElementName = "StackPanelTest";
            m_stackPanel.Initialize();
            Application.AddComponent(m_stackPanel);

            AgBehavior bg = new AgBehavior();
            bg.ElementName = "GdBg";
            
            bg.Plugin = Plugin;
            bg.Initialize();
            Application.AddComponent(bg);

            center = new AgBehavior();
            center.ElementName = "GdCenter";
            center.Plugin = Plugin;
            center.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Center);
            center.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Center);
            center.Initialize();
            Application.AddComponent(center);

            top = new AgBehavior();
            top.ElementName = "GdTop";
            top.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Center);
            top.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Top);
            top.SetNamedValue(Grid.MarginTop, 3);
            top.Plugin = Plugin;
            top.Initialize();
            Application.AddComponent(top);

            bottom = new AgBehavior();
            bottom.ElementName = "GdBottom";
            bottom.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Center);
            bottom.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Bottom);
            bottom.SetNamedValue(Grid.MarginBottom, 3);
            bottom.Plugin = Plugin;
            bottom.Initialize();
            Application.AddComponent(bottom);

            left = new AgBehavior();
            left.ElementName = "GdLeft";
            left.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Left);
            left.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Center);
            left.SetNamedValue(Grid.MarginLeft, 3);
            left.Plugin = Plugin;
            left.Initialize();
            Application.AddComponent(left);

            right = new AgBehavior();
            right.ElementName = "GdRight";
            right.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Right);
            right.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Center);
            right.SetNamedValue(Grid.MarginRight, 3);
            right.Plugin = Plugin;
            right.Initialize();
            Application.AddComponent(right);

            gd = new Grid();
            gd.Plugin = Plugin;
            gd.ElementName = "GridSample";
            gd.Initialize();
            Application.AddComponent(gd);
        }

        void onSelectionChanged(object sender, sea args)
        {
            ArrayList selection = ((ToggleButtonList)sender).SelectedItems;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < selection.Length; i++)
            {
                sb.Append(((ToggleButton)selection[i]).ID + ";");
            }
            m_debugBox.Text = sb.ToString();
        }
        void onBtnClick(object sender, sea args)
        {
            m_debugBox.Text = "Click ! "+ ((Button)sender).ID;
            m_stackPanel.Width = 200;

            AgBehavior oldCenter = center;
            center = left;
            left = bottom;
            bottom = right;
            right = top;
            top = oldCenter;

            gd.SuspendLayout();

            center.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Center);
            center.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Center);
            center.SetNamedValue(Grid.MarginBottom, 0);
            center.SetNamedValue(Grid.MarginTop, 0);
            center.SetNamedValue(Grid.MarginLeft, 0);
            center.SetNamedValue(Grid.MarginRight, 0);

            top.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Center);
            top.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Top);
            top.SetNamedValue(Grid.MarginTop, 3);
            top.SetNamedValue(Grid.MarginBottom, 0);
            top.SetNamedValue(Grid.MarginLeft, 0);
            top.SetNamedValue(Grid.MarginRight, 0);

            bottom.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Center);
            bottom.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Bottom);
            bottom.SetNamedValue(Grid.MarginBottom, 3);
            bottom.SetNamedValue(Grid.MarginTop, 0);
            bottom.SetNamedValue(Grid.MarginLeft, 0);
            bottom.SetNamedValue(Grid.MarginRight, 0);

            left.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Left);
            left.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Center);
            left.SetNamedValue(Grid.MarginLeft, 3);
            left.SetNamedValue(Grid.MarginBottom, 0);
            left.SetNamedValue(Grid.MarginTop, 0);
            left.SetNamedValue(Grid.MarginRight, 0);

            right.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Right);
            right.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Center);
            right.SetNamedValue(Grid.MarginRight, 3);
            right.SetNamedValue(Grid.MarginBottom, 0);
            right.SetNamedValue(Grid.MarginTop, 0);
            right.SetNamedValue(Grid.MarginLeft, 0);

            gd.ResumeLayout();
        }

        void onPushed(object sender, sea args)
        {
            m_debugBox.Text = "Pushed !";
        }

        void onUnPushed(object sender, sea args)
        {
            m_debugBox.Text = "UnPushed !";
        }


    }
}
